package com.example.robertluozizhao.framecollectdemo.activity;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;

import com.example.robertluozizhao.framecollectdemo.R;
import com.example.robertluozizhao.framecollectdemo.view.SwitchButtonView;

/**
 * Created by robert.luozizhao on 2018/1/10.
 */

public class CustomSwitchButtonActivity extends AppCompatActivity {


    private SwitchButtonView mSbv;
    private AsyncTask<Void, Void, Void> asyncTask;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_switchbutton);
        mSbv = (SwitchButtonView) findViewById(R.id.sbv);
        mSbv.setBall(R.drawable.icon_ball);
        mSbv.setBtnSwitchOn(R.drawable.icon_switch_on_32);
        mSbv.setBtnSwitchOff(R.drawable.icon_switch_off_32);
        mSbv.setToggleState(false);
        mSbv.setToggleState(new SwitchButtonView.ToggleState() {
            @Override
            public void ShowToggleState(SwitchButtonView switchButtonView, boolean isOpen) {
                Toast.makeText(CustomSwitchButtonActivity.this,switchButtonView.getToggleState()+"",Toast.LENGTH_SHORT).show();

                startAsyncTask();//leakCanary内存泄露测试代码


            }
        });
    }


    void startAsyncTask() {

        // This async task is an anonymous class and therefore has a hidden reference to the outer
        // class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation),
        // the activity instance will leak.
        // Do some slow work in background
        asyncTask = new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                // Do some slow work in background
                SystemClock.sleep(200000);
                return null;
            }
        };
        asyncTask.execute();
        Toast.makeText(this, "请关闭这个A完成泄露", Toast.LENGTH_SHORT).show();
    }
}
